/*

 * socket_client.c
 *
 *  Created on: Mar 15, 2014
 *      Author: nerohwang
 */
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>       //pthread_t , pthread_attr_t and so on.
#include<stdio.h>
#include<netinet/in.h>      //structure sockaddr_in
#include<arpa/inet.h>       //Func : htonl; htons; ntohl; ntohs
#include<assert.h>          //Func :assert
#include<string.h>          //Func :memset
#include<unistd.h>          //Func :close,write,read
#define SOCK_PORT 13227
#define BUFFER_LENGTH 1024
int main()
{
    int sockfd;
    int tempfd;
    FILE *fp=NULL;
    struct sockaddr_in s_addr_in;
    char data_send[BUFFER_LENGTH]="aa";
    char data_recv[BUFFER_LENGTH];
    memset(data_send,0,BUFFER_LENGTH);
    memset(data_recv,0,BUFFER_LENGTH);

    sockfd = socket(AF_INET,SOCK_STREAM,0);       //ipv4,TCP
    if(sockfd == -1)
    {
        fprintf(stderr,"socket error!\n");
        exit(1);
    }

    //before func connect, set the attr of structure sockaddr.
    memset(&s_addr_in,0,sizeof(s_addr_in));
    s_addr_in.sin_addr.s_addr = inet_addr("127.0.0.1");      //trans char * to in_addr_t
    s_addr_in.sin_family = AF_INET;
    s_addr_in.sin_port = htons(SOCK_PORT);

    tempfd = connect(sockfd,(struct sockaddr *)(&s_addr_in),sizeof(s_addr_in));
    if(tempfd == -1)
    {
        fprintf(stderr,"Connect error! \n");
        exit(1);
    }

    while(1)
    {
        if((fp=fopen("test2.txt","r"))==NULL)
	{
		printf("file no found");
		break;
	}
	char buffer[1024]="";
	printf("Please input something you wanna say(input \"quit\" to quit):\n");
        //scanf("%s",&data_send);
	int ret;     
   //scanf("%[^\n]",data_send);         //or you can also use this
	fseek(fp,0,SEEK_END);
	int len = ftell(fp);
	rewind(fp);
	//printf("%ld\n",len);
	while(1)
        {
//	len-=1024;
//printf("%d\n",len);

	if(len<=0)
	break;
	ret=fgets(buffer,1024,fp);
//	printf("%d\n",ret);
//		printf("%s",buffer);
//		printf("bbb\n");
		tempfd = write(sockfd,buffer,strlen(buffer));
//		printf("%d\n",tempfd);
        	if(tempfd<=0)
        	{
            		printf("error\n");
            		break;
        	}
	len-=strlen(buffer);
	bzero(buffer,1024);

	}
	//fread(buffer,1,len,fp);
	//write(sockfd,buffer,strlen(buffer));
	printf("finish send\n");
	if(shutdown(sockfd,1)==1)
	printf("shutdown writting");
        if(strcmp(data_send,"quit") == 0)  //quit,write the quit request and shutdown client
        {
            break;
        }
        else
        while(1){
            tempfd = read(sockfd,data_recv,BUFFER_LENGTH);
            if(tempfd<=0)
		exit(0);
		//assert(tempfd != -1);
            printf("%s\n",data_recv);
            memset(data_send,0,BUFFER_LENGTH);
            memset(data_recv,0,BUFFER_LENGTH);
        }
    }

    int ret = shutdown(sockfd,SHUT_WR);       //or you can use func close()--<unistd.h> to close the fd
    assert(ret != -1);
    return 0;
}
